package com.webmagic.webmagic.processor;

import com.webmagic.webmagic.Scheduler.MyRedisScheduler;

import java.util.List;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.RedisScheduler;

/**
 * @author xbl
 * @Description
 * @date 2019/1/7
 */
public class CsdnPageProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36");

    @Override
    public void process(Page page) {
        List<String> all = page.getHtml().xpath("//h2/a/@href").all();
        page.addTargetRequests(all);
    }

    @Override
    public Site getSite() {
        return this.site;
    }

    public static void main(String[] args) {
        MyRedisScheduler myRedisScheduler = new MyRedisScheduler("127.0.0.1");
        myRedisScheduler.setFirst(false);
        Spider.create(new CsdnPageProcessor()).addUrl("https://hacpai.com/tag/Java").setScheduler(myRedisScheduler).thread(5).start();
    }
}
